package com.oe.ldmrabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {

    @Bean
    public Queue queueTTLWaiting() {
        Map<String, Object> props = new HashMap<>();
       // 对于该队列中的消息，设置都等待30分钟
        props.put("x-message-ttl", 10000);
        props.put("x-dead-letter-exchange", "ex.order.dlx");
        props.put("x-dead-letter-routing-key", "order.dlx");
        Queue queue = new Queue("queue.order.ttl-waiting", true, false,
                false, props);
        return queue;
    }

    @Bean
    public Queue queueDlx() {
        Queue queue = new Queue("queue.order.dlx", true, false, false);
        return queue;
    }

    @Bean
    public Exchange exchangeTTLWaiting() {
        DirectExchange exchange = new DirectExchange("ex.order.ttl-waiting", true,
                false, null);
        return exchange;
    }

    @Bean
    public Exchange exchangeDlx() {
        DirectExchange exchange = new DirectExchange("ex.order.dlx", true,
                false, null);
        return exchange;
    }

    @Bean
    public Binding bindingTTLWaiting() {
       return BindingBuilder.bind(queueTTLWaiting()).to(exchangeTTLWaiting()).with("order.ttl").noargs();
    }

    @Bean
    public Binding bindingDlx() {
        return BindingBuilder.bind(queueDlx()).to(exchangeDlx()).with("order.dlx").noargs();
    }
}
